package com.dragon.geekbang.thinking.in.spring.ioc.container.overview.container;

import com.dragon.geekbang.thinking.in.spring.ioc.container.overview.domain.Daddy;
import com.dragon.geekbang.thinking.in.spring.ioc.container.overview.domain.User;
import lombok.Data;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 支持 Annotation等特性
 */
public class AnnotaionApplicationContextAsIocContainerDemo {
    public static void main(String[] args) {

        // 创建 ApplicationContext
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        // 将当前类注册为配置
        applicationContext.register(AnnotaionApplicationContextAsIocContainerDemo.class);

        // 刷新(启动)上下文
        applicationContext.refresh();

        User user = applicationContext.getBean(User.class);

        System.out.println(user);

        Daddy daddy = applicationContext.getBean(Daddy.class);

        System.out.println(daddy);

        // 关闭上下文
        applicationContext.close();

    }

    @Bean
    public User createUser(){
        User user = User.builder()
                .id(1L)
                .name("李莱")
                .build();
        return user;
    }

    @Bean
    public Daddy createDaddy(User user){
        Daddy daddy = new Daddy();
        daddy.setName("李昌龙");
        daddy.setUser(user);
        return daddy;
    }
}
